'use strict';
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const b = require('../nodes/Scalar.cjs');
function E(h, f, u) {
  const i = h.offset,
    l = I(h, f, u);
  if (!l) return { value: '', type: null, comment: '', range: [i, i, i] };
  const d = l.mode === '>' ? b.Scalar.BLOCK_FOLDED : b.Scalar.BLOCK_LITERAL,
    t = h.source ? N(h.source) : [];
  let g = t.length;
  for (let e = t.length - 1; e >= 0; --e) {
    const c = t[e][1];
    if (c === '' || c === '\r') g = e;
    else break;
  }
  if (g === 0) {
    const e =
      l.chomp === '+' && t.length > 0
        ? `
`.repeat(Math.max(1, t.length - 1))
        : '';
    let c = i + l.length;
    return (
      h.source && (c += h.source.length),
      { value: e, type: d, comment: l.comment, range: [i, c, c] }
    );
  }
  let r = h.indent + l.indent,
    k = h.offset + l.length,
    p = 0;
  for (let e = 0; e < g; ++e) {
    const [c, a] = t[e];
    if (a === '' || a === '\r')
      l.indent === 0 && c.length > r && (r = c.length);
    else {
      if (c.length < r) {
        const S =
          'Block scalars with more-indented leading empty lines must use an explicit indentation indicator';
        u(k + c.length, 'MISSING_CHAR', S);
      }
      l.indent === 0 && (r = c.length), (p = e);
      break;
    }
    k += c.length + a.length + 1;
  }
  for (let e = t.length - 1; e >= g; --e) t[e][0].length > r && (g = e + 1);
  let o = '',
    n = '',
    s = !1;
  for (let e = 0; e < p; ++e)
    o +=
      t[e][0].slice(r) +
      `
`;
  for (let e = p; e < g; ++e) {
    let [c, a] = t[e];
    k += c.length + a.length + 1;
    const S = a[a.length - 1] === '\r';
    if ((S && (a = a.slice(0, -1)), a && c.length < r)) {
      const B = `Block scalar lines must not be less indented than their ${
        l.indent ? 'explicit indentation indicator' : 'first line'
      }`;
      u(k - a.length - (S ? 2 : 1), 'BAD_INDENT', B), (c = '');
    }
    d === b.Scalar.BLOCK_LITERAL
      ? ((o += n + c.slice(r) + a),
        (n = `
`))
      : c.length > r || a[0] === '	'
        ? (n === ' '
            ? (n = `
`)
            : !s &&
              n ===
                `
` &&
              (n = `

`),
          (o += n + c.slice(r) + a),
          (n = `
`),
          (s = !0))
        : a === ''
          ? n ===
            `
`
            ? (o += `
`)
            : (n = `
`)
          : ((o += n + a), (n = ' '), (s = !1));
  }
  switch (l.chomp) {
    case '-':
      break;
    case '+':
      for (let e = g; e < t.length; ++e)
        o +=
          `
` + t[e][0].slice(r);
      o[o.length - 1] !==
        `
` &&
        (o += `
`);
      break;
    default:
      o += `
`;
  }
  const m = i + l.length + h.source.length;
  return { value: o, type: d, comment: l.comment, range: [i, m, m] };
}
function I({ offset: h, props: f }, u, i) {
  if (f[0].type !== 'block-scalar-header')
    return i(f[0], 'IMPOSSIBLE', 'Block scalar header not found'), null;
  const { source: l } = f[0],
    d = l[0];
  let t = 0,
    g = '',
    r = -1;
  for (let n = 1; n < l.length; ++n) {
    const s = l[n];
    if (!g && (s === '-' || s === '+')) g = s;
    else {
      const m = Number(s);
      !t && m ? (t = m) : r === -1 && (r = h + n);
    }
  }
  r !== -1 &&
    i(
      r,
      'UNEXPECTED_TOKEN',
      `Block scalar header includes extra characters: ${l}`,
    );
  let k = !1,
    p = '',
    o = l.length;
  for (let n = 1; n < f.length; ++n) {
    const s = f[n];
    switch (s.type) {
      case 'space':
        k = !0;
      case 'newline':
        o += s.source.length;
        break;
      case 'comment':
        u &&
          !k &&
          i(
            s,
            'MISSING_CHAR',
            'Comments must be separated from other tokens by white space characters',
          ),
          (o += s.source.length),
          (p = s.source.substring(1));
        break;
      case 'error':
        i(s, 'UNEXPECTED_TOKEN', s.message), (o += s.source.length);
        break;
      default: {
        const m = `Unexpected token in block scalar header: ${s.type}`;
        i(s, 'UNEXPECTED_TOKEN', m);
        const e = s.source;
        e && typeof e == 'string' && (o += e.length);
      }
    }
  }
  return { mode: d, indent: t, chomp: g, comment: p, length: o };
}
function N(h) {
  const f = h.split(/\n( *)/),
    u = f[0],
    i = u.match(/^( *)/),
    d = [i != null && i[1] ? [i[1], u.slice(i[1].length)] : ['', u]];
  for (let t = 1; t < f.length; t += 2) d.push([f[t], f[t + 1]]);
  return d;
}
exports.resolveBlockScalar = E;
